﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SerialBrowserBridge.Devices;

namespace SerialBrowserBridge.WebServer.Handler
{
    /// <summary>
    /// Handles all unspecific requests based on a buffer
    /// </summary>
    public class CommunicationHandler: IWebHandler
    {
        private String buffer = String.Empty;
        private IBlockDevice device; 

        public void Initialize(WebServerCore webServer)
        {
            device = webServer.Device;
            device.ReadBlock += text => buffer = text;
        }

        public Object Process(Request request, Response response)
        {
            var url = request.URL.Trim('/');

            response.Write(buffer);

            if (!String.IsNullOrEmpty(url))
                device.WriteBlock(url);

            return new HandleAction { Handled = true };
        }

        public int Priority
        {
            get { return 0; }
        }
    }
}
